//
//  DayNightImgView.m
//  one·一个  tyw
//
//  Created by tyw on 15/8/28.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "DayNightImgView.h"
#import "DayNightManager.h"

@implementation DayNightImgView

/**
 *  日间夜间切换有学问，很多图片的命名都有规律，请注意。待改进.   完全可以效仿微博项目中的主题切换管家   拼接字符串，返回图片
 */

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        //接收通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dayNightNotification:) name:kDayNightTypeChangedNotification object:nil];
    }
    return self;
}

- (void)awakeFromNib {
    //添加通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dayNightNotification:) name:kDayNightTypeChangedNotification object:nil];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kDayNightTypeChangedNotification object:nil];
}

- (void)setImgName:(NSString *)imgName {
    if (![_imgName isEqualToString:imgName]) {
        _imgName = imgName;
        
        [self dayNightNotification:nil];
    }
}

//接收到通知后调用的方法，日间夜间模式切换，改变文本字体颜色
- (void)dayNightNotification:(NSNotification*)notification {
    //通过管家获得图片
    DayNightManager* manager = [DayNightManager sharedManager];
    
    UIImage* image = [manager getThemeImage:_imgName];
    
    //拉伸点设置（为了方便转发的微博的背景图片的样式）
    image = [image stretchableImageWithLeftCapWidth:_leftCapWidth topCapHeight:_topCapWidth];
    
    self.image = image;
}

@end
